"""
浏览一个需要登录才能看的url
1)请求头从包含cookie信息
  F12——network  找到请求头user-agent、cookie
2）cookies参数

"""
import requests

# 浏览一个需要登录才能看的url，查看Gitee上我的所有项目
url = "https://gitee.com/pyt5/projects"
# 1）没有加cookie 看到的页面效果是没登录的效果
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
response = requests.get(url, headers=headers)
with open('no_cookie.html', 'w', encoding='utf-8') as f:
    # decode()默认解码utf-8
    f.write(response.content.decode())

# 2）加了cookie（网页上登陆之后拷贝请求头中的cookie），看到效果会不一样
# 注意：构造字典，字典中元素之间用逗号分隔
headers2 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
    'Cookie': 'user_locale=zh-CN; oschina_new_user=false; tz=Asia%2FShanghai; sajssdk_2015_cross_new_user=1; Hm_lvt_24f17767262929947cc3631f99bfd274=1606358635; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221714279%22%2C%22first_id%22%3A%22176026f92da250-0af497d2596d7e-333769-2073600-176026f92dba2f%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%22176026f92da250-0af497d2596d7e-333769-2073600-176026f92dba2f%22%7D; Hm_lpvt_24f17767262929947cc3631f99bfd274=1606359038; gitee-session-n=ejk2ZUlhOXlSLzZUSHNwUmk1ajM2aG9LbjdFWUk3Q2o5OEVyNTViTWdNaXZ1aDhWOVVob0dHSUxVS0hLOHJ3aExMYkVxdVZBd3g4ZDIyMm56Zk1DZm9nNmtrdEk5Uk1zOE1ST1dVVFlRRDVHRVBFYzY2Q05pdzJLVWdNSzVLUld2MXZTeDdqQitENXh4Uk9uRWZEUkhsMTRURWpYS1BFdFJjemJHSVlCU1NrZDQyUVloUFRla0Y2ajZyUUt2UDBqa21RV1BZTDlvWXFYMkczaUUyelhkUXk0Z0w1VW9WdzlrYTV3ZnkrWUJ5cjg5YWNpSE5BS25rNUlxOGxScVM4THg4NFcwUFRYS0RPOENCaUFEeGxBVE8zS1F3MEkxaEhSRU9GMDk2ekRhNnZVZjBtMGl6NUVkZWdkVzQxQUwrUHQ1VS9QTG83MHpDb2ZwL3FCbGFaaTV3YUw1SThXYXRZTWVqK3YvOTNaejU4dk1WSmVKUXN0R1BYYnIyWnRYUW1DLS16ejVXMWdiRnltUVZRL0hVbGVEYk5nPT0%3D--56856b95901403564cdfe4281413b7c2f2f36d2c'
}
response = requests.get(url, headers=headers2)
with open('has_cookie.html', 'w', encoding='utf-8') as f:
    f.write(response.content.decode())
